package net.lab1024.smartadmin.module.business.gearboxSpeed.controller;

import net.lab1024.smartadmin.common.domain.PageResultDTO;
import net.lab1024.smartadmin.common.controller.BaseController;
import net.lab1024.smartadmin.common.domain.ResponseDTO;
import net.lab1024.smartadmin.common.domain.ValidateList;
import net.lab1024.smartadmin.module.business.gearboxEfficiency.domain.entity.GearboxEfficiencyEntity;
import net.lab1024.smartadmin.module.business.gearboxSpeed.domain.dto.GearboxSpeedAddDTO;
import net.lab1024.smartadmin.module.business.gearboxSpeed.domain.dto.GearboxSpeedUpdateDTO;
import net.lab1024.smartadmin.module.business.gearboxSpeed.domain.dto.GearboxSpeedQueryDTO;
import net.lab1024.smartadmin.module.business.gearboxSpeed.domain.entity.GearboxSpeedEntity;
import net.lab1024.smartadmin.module.business.gearboxSpeed.domain.vo.GearboxSpeedVO;
import net.lab1024.smartadmin.module.business.gearboxSpeed.domain.vo.GearboxSpeedExcelVO;
import net.lab1024.smartadmin.module.business.gearboxSpeed.service.GearboxSpeedService;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import org.apache.poi.ss.usermodel.Workbook;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * [ 变速箱速比参数 ]
 *
 * @author wuhq
 * @version 1.0
 * @company 汽车传动
 * @copyright (c)  汽车传动Inc. All rights reserved.
 * @date 2022-05-27 11:30:30
 * @since JDK1.8
 */
@RestController
@Api(tags = {"变速箱速比参数"})
public class GearboxSpeedController extends BaseController {

    @Autowired
    private GearboxSpeedService gearboxSpeedService;

    @ApiOperation(value = "分页查询变速箱速比参数",notes = "@author wuhq")
    @PostMapping("/gearboxSpeed/page/query")
    public ResponseDTO<PageResultDTO<GearboxSpeedVO>> queryByPage(@RequestBody GearboxSpeedQueryDTO queryDTO) {
        return gearboxSpeedService.queryByPage(queryDTO);
    }

    @ApiOperation(value = "根据变速箱id获取变速表数据",notes = "@author wuhq")
    @GetMapping("/gearboxSpeed/getByGearboxId/{gearboxId}")
    public ResponseDTO<GearboxSpeedEntity> getByGearboxId(@PathVariable Long gearboxId) {
        return ResponseDTO.succData(gearboxSpeedService.getByGearboxId(gearboxId));
    }

    @ApiOperation(value = "添加变速箱速比参数",notes = "@author wuhq")
    @PostMapping("/gearboxSpeed/add")
    public ResponseDTO<String> add(@RequestBody @Validated GearboxSpeedAddDTO addTO){

        // 保存之前先删除旧的数据
        gearboxSpeedService.delByGearboxId(addTO.getGearboxId());

        return gearboxSpeedService.add(addTO);
    }

    @ApiOperation(value="修改变速箱速比参数",notes = "@author wuhq")
    @PostMapping("/gearboxSpeed/update")
    public ResponseDTO<String> update(@RequestBody @Validated GearboxSpeedUpdateDTO updateDTO){
        return gearboxSpeedService.update(updateDTO);
    }

    @ApiOperation(value="批量删除变速箱速比参数",notes = "@author wuhq")
    @PostMapping("/gearboxSpeed/deleteByIds")
    public ResponseDTO<String> delete(@RequestBody @Validated ValidateList<Long> idList) {
        return gearboxSpeedService.deleteByIds(idList);
    }

    @ApiOperation(value = "批量导出", notes = "@author wuhq")
    @PostMapping("/gearboxSpeed/export/batch")
    public void batchExport(@RequestBody @Validated ValidateList<Long> idList, HttpServletResponse response) {
        //查询数据
        List<GearboxSpeedExcelVO> gearboxSpeedList = gearboxSpeedService.queryBatchExportData(idList);
        //导出操作
        ExportParams ex = new ExportParams("变速箱速比参数", "Sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(ex, GearboxSpeedExcelVO.class, gearboxSpeedList);
        downloadExcel("变速箱速比参数", workbook, response);
    }

    @ApiOperation(value = "导出全部", notes = "@author wuhq")
    @PostMapping("/gearboxSpeed/export/all")
    public void exportAll(@RequestBody @Validated GearboxSpeedQueryDTO queryDTO, HttpServletResponse response) {
        //查询数据
        List<GearboxSpeedExcelVO> gearboxSpeedList = gearboxSpeedService.queryAllExportData(queryDTO);
        //导出操作
        ExportParams ex = new ExportParams("变速箱速比参数", "Sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(ex, GearboxSpeedExcelVO.class, gearboxSpeedList);
        downloadExcel("变速箱速比参数", workbook, response);
    }

}
